package cn.edu.sdjzu.xg11.dao;

import java.util.Collection;
import java.util.TreeSet;

import org.springframework.stereotype.Repository;

import cn.edu.sdjzu.xg11.domain.Title;
@Repository
public final class TitleDao {
	private static Collection<Title> titles;
	static{
		titles = new TreeSet<Title>();
		Title title = new Title(1,"教授","01","");
		titles.add(title);
		titles.add(new Title(2,"副教授","02",""));
		titles.add(new Title(3,"讲师","03",""));
		titles.add(new Title(4,"助教","04",""));
	}
	public Collection<Title> getTitles(){
		return TitleDao.titles;
	}
	
	public Title getTitle(Integer id){
		Title desiredTitle = null;
		for (Title title : titles) {
			if(id.equals(title.getId())){
				desiredTitle =  title; 
				break;
			}
		}
		return desiredTitle;
	}
	
	public boolean updateTitle(Title title){
		titles.remove(title);
		return titles.add(title);		
	}
	
	public boolean addTitle(Title title){
		return titles.add(title);		
	}

	public boolean deleteTitle(Integer id){
		Title title = this.getTitle(id);
		return this.deleteTitle(title);
	}
	
	public boolean deleteTitle(Title title){
		return titles.remove(title);
	}
	
	
	public static void main(String[] args){
		TitleDao dao = new TitleDao();
		Collection<Title> titles = dao.getTitles();
		display(titles);
		Title title = new Title(2,"副教授","02","");
		title.setNo("02x");
		dao.updateTitle(title);
		display(titles);
	
		dao.deleteTitle(title);
		dao.deleteTitle(3);
		display(titles);
		dao.addTitle(new Title(2,"工程师","04",""));
		display(titles);
		System.out.println("2="+dao.getTitle(2));
	}

	private static void display(Collection<Title> titles) {
		for (Title title : titles) {
			System.out.println(title);
		}
	}
	
	
}
